package com.ybkj.daijia.api.weixin.handler.impl;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.WxSetting;
import com.ybkj.daijia.api.weixin.WxMsgType;
import com.ybkj.daijia.api.weixin.entity.WxBaseMsgEntity;
import com.ybkj.daijia.api.weixin.entity.WxBaseRespEntity;
import com.ybkj.daijia.api.weixin.entity.WxItemPicDescEntity;
import com.ybkj.daijia.api.weixin.entity.WxMsgEventEntity;
import com.ybkj.daijia.api.weixin.entity.WxRespPicDescEntity;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WxPriceTextHandler extends AbstractWxEventHandler {

    @Autowired
    private SettingUtils settingUtils;


    @Override
    public boolean canHandle(WxBaseMsgEntity msg) {
        if (super.canHandle(msg)) {
            if ("price".equals(((WxMsgEventEntity) msg).getEventKey())) {
                return true;
            }
        }

        return false;
    }


    @Override
    public WxBaseRespEntity handleEvent(WxMsgEventEntity msg,
        Map<String, Object> context, WxBaseRespEntity resp) {
        //图文消息
        WxRespPicDescEntity news = new WxRespPicDescEntity();

        List<WxItemPicDescEntity> articles = new LinkedList<WxItemPicDescEntity>();

        WxItemPicDescEntity item = new WxItemPicDescEntity();

        WxSetting setting = settingUtils.getWx();

        String pic = String.format("%sassets/img/price_table.png", setting.getWxServer());
        String url = String.format("%smember/api/rest/v3/listAreas", setting.getWxServer());

        item.setTitle("代驾服务价格表");
        item.setPicUrl(pic);
        item.setUrl(url);
        item.setDescription("代驾服务价格表");
        articles.add(item);

        news.setArticles(articles);
        news.setCreatedDate(new Date());
        news.setCreateTime(news.getCreatedDate().getTime());
        news.setFromUserName(msg.getToUserName());
        news.setToUserName(msg.getFromUserName());
        news.setFuncFlag(0);
        news.setMsgType(WxMsgType.news.name());

        return news;
    }

}
